<template>
  <div class="login-view">
    <div class="login-container">
      <!-- 蓝色头部区域 -->
      <div class="login-header">
        <div class="header-decoration">
          <div class="circle circle-1"></div>
          <div class="circle circle-2"></div>
        </div>
        <h1 class="game-title">涂色抢地盘</h1>
        <p class="game-subtitle">Territory Paint Battle</p>
      </div>
      
      <!-- 白色表单区域 -->
      <div class="login-form">
        <el-form 
          :model="loginForm" 
          :rules="loginRules" 
          ref="loginFormRef" 
          label-position="top"
          @keyup.enter="handleLogin"
        >
          <el-form-item label="玩家名称或邮箱" prop="username">
            <el-input 
              v-model="loginForm.username" 
              placeholder="输入你的玩家名称或邮箱"
              :prefix-icon="User"
              clearable
              size="large"
            />
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input 
              v-model="loginForm.password" 
              type="password"
              placeholder="输入密码"
              :prefix-icon="Lock"
              clearable
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              size="large" 
              style="width: 100%"
              @click="handleLogin"
              :loading="false"
              :disabled="!isFormValid"
            >
              <el-icon v-if="true"><Brush /></el-icon>
              {{ false ? '登录中...' : '开始涂色!' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <!-- 导航链接 -->
        <div class="login-links">
          <div class="links-row">
            <el-link 
              type="primary" 
              @click="forgotPassword"
              class="link-item"
            >
              忘记密码?
            </el-link>
            <el-link 
              type="primary" 
              @click="guestPlay"
              class="link-item"
            >
              游客试玩
            </el-link>
          </div>
          
          <div class="register-link">
            <el-link 
              type="primary" 
              @click="goToRegister"
              class="register-text"
            >
              还没有领地? 创建账号扩张领地
            </el-link>
          </div>
        </div>
        
        <!-- 登录小技巧 -->
        <div class="login-tips">
          <div class="tips-header">
            <el-icon class="tips-icon"><Location /></el-icon>
            <span class="tips-title">登录小技巧</span>
          </div>
          <p class="tips-content">
            快速登录,抢占先机!空白区域正在等待涂色,早登录早占领!
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
// 页面职责：提供登录表单，使用 user store 模拟登录并持久化，成功后跳转首页
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Brush, Location } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 表单引用
const loginFormRef = ref()

// 登录表单数据
const loginForm = reactive({ username: 'admin', password: '123456' })

// 表单验证状态
const isFormValid = computed(() => loginForm.username.trim() && loginForm.password.trim())

// 登录表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入玩家名称或邮箱', trigger: 'blur' },
    { min: 1, max: 50, message: '用户名长度在 1 到 50 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    const credentials = { 
      username: loginForm.username.trim(), 
      password: loginForm.password 
    }
    
    await userStore.login(credentials)
    router.push('/')
  } catch (error) {
    console.error('登录失败:', error)
    // 错误消息已经在store中处理
  }
}

// 忘记密码
const forgotPassword = () => { 
  router.push('/forgot-password')
}

// 游客试玩
const guestPlay = async () => {
  try {
    ElMessage.success('已进入游客模式，开始试玩！')
    router.push('/lobby')
  } catch (error) {
    console.error('进入游客模式失败:', error)
    ElMessage.error('进入游客模式失败')
  }
}

// 跳转到注册页面
const goToRegister = () => { router.push('/register') }

// 页面加载时检查登录状态
onMounted(async () => {
  await userStore.init()
  
  // 如果已经登录，直接跳转到首页
  if (userStore.isAuthenticated) {
    router.push('/')
  }
})
</script>

<style scoped>
.login-view {
  min-height: 100vh;
  height: 100vh;
  background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.login-container {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  max-width: 450px;
  width: 100%;
  margin: 0 auto;
  position: relative;
}

/* 蓝色头部区域 */
.login-header {
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  padding: 40px 30px 30px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.header-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 80px;
  height: 80px;
  top: -20px;
  left: -20px;
}

.circle-2 {
  width: 60px;
  height: 60px;
  top: -10px;
  right: -10px;
}

.game-title {
  color: white;
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  text-align: center;
}

.game-subtitle {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  margin: 0;
  font-weight: 400;
  text-align: center;
}

/* 白色表单区域 */
.login-form {
  padding: 40px 30px;
  text-align: center;
}

:deep(.el-form) {
  text-align: center;
}

:deep(.el-form-item) {
  text-align: center;
  margin-bottom: 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
  margin-bottom: 8px;
  text-align: center;
  display: block;
}

:deep(.el-form-item__content) {
  text-align: center;
  display: flex;
  justify-content: center;
}

:deep(.el-input) {
  width: 100%;
  max-width: 350px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #1890ff;
}

:deep(.el-button) {
  border-radius: 8px;
  font-weight: 500;
  height: 48px;
  transition: all 0.3s ease;
  width: 100%;
  max-width: 350px;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  border: none;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(24, 144, 255, 0.3);
}

:deep(.el-button--primary:disabled) {
  background: #c0c4cc;
  transform: none;
  box-shadow: none;
}

/* 导航链接 */
.login-links {
  margin-top: 24px;
  text-align: center;
}

.links-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  max-width: 350px;
  margin-left: auto;
  margin-right: auto;
}

.link-item {
  font-size: 14px;
  text-decoration: none;
  text-align: center;
}

.register-link {
  text-align: center;
  margin-bottom: 24px;
}

.register-text {
  font-size: 14px;
  text-decoration: none;
  text-align: center;
}

/* 登录小技巧 */
.login-tips {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  border-left: 4px solid #1890ff;
  text-align: center;
  max-width: 350px;
  margin: 0 auto;
}

.tips-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 8px;
}

.tips-icon {
  color: #1890ff;
  font-size: 16px;
}

.tips-title {
  color: #1890ff;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
}

.tips-content {
  color: #606266;
  font-size: 13px;
  line-height: 1.5;
  margin: 0;
  text-align: center;
}

@media (max-width: 768px) {
  .login-view {
    padding: 10px;
  }
  
  .login-container {
    margin: 10px auto;
  }
  
  .login-header {
    padding: 30px 20px 20px;
  }
  
  .login-form {
    padding: 30px 20px;
  }
  
  .game-title {
    font-size: 28px;
  }
  
  .game-subtitle {
    font-size: 14px;
  }
  
  .links-row {
    flex-direction: column;
    gap: 12px;
    align-items: center;
  }
}

@media (max-width: 480px) {
  .login-view {
    padding: 10px;
  }
  
  .login-header {
    padding: 25px 15px 15px;
  }
  
  .login-form {
    padding: 25px 15px;
  }
  
  .game-title {
    font-size: 24px;
  }
  
  .links-row {
    flex-direction: column;
    gap: 12px;
    align-items: center;
  }
  
  .login-tips {
    max-width: 100%;
  }
}
</style>
